<?php

use Biz\service\Store\StoreUserConsult;
use Biz\service\Store\StoreHomeService;
use Doraemon\model\Member\WxaPhoneModel;
use Doraemon\model\Phone;
use Doraemon\tools\Crypt;
use Biz\cache\store\StoreCallAndReserve;
use Doraemon\service\Login;
use Doraemon\service\Member\PhoneCrypto;
use Doraemon\model\Member\WxaModel;
use hhz\constant\redis\redisKeys;
use Hhz\Redis\otherRedis;


class ReserveInfoAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;
    const DEFAULT_RESERVE_LIMIT = 5;
    const DEFAULT_ERROR_CODE = self::CODE_ERROR_VIEW_TOSTA;
    const DECRYPT_PHONE = 'magic_mirror_decrypt_getphone';

    //参数校验-规则
    public const VALIDATOR_RULES = [
        'store_id' => 'require|integer',
    ];

    /**
     * @throws Exception
     */
    public function main()
    {
        $store_id = $this->params['store_id'];
        $act_from = $this->params['type'] ?? '';

        //先判断该用户是否已经预约留咨过五家不同的门店,如果是提示已经预约过多
        $user_reserve_info = StoreCallAndReserve::getUserReserveStoreid($this->uid);
        //迁移
//        $constKey = redisKeys::KEY_B_OTHER_USER_STORE_RESERVE_;
//        $redis = new otherRedis($constKey);
//        $key = $constKey.$this->uid;
//        $user_reserve_info = $redis->smembers($key);


        if ($user_reserve_info) {
            $reserve_num = count($user_reserve_info);
            //如果数量满足,并且该门店id不再已留咨的uid数组里的话,弹窗
            if (self::DEFAULT_RESERVE_LIMIT <= $reserve_num && !in_array($store_id, $user_reserve_info)) {
                throw new \Exception("抱歉，已超过当日留资上限", self::DEFAULT_ERROR_CODE);
            }
        }

        $consult_info = StoreUserConsult::getConsultInfo($store_id, $this->uid);
        if ($act_from == 'StoreServices' || $act_from == 'WikiInfoDetail') {
            $consult_info['store_activity'] = self::getSaleList($store_id);
        }

        if (in_array($this->appType,
                [self::APPTYPE_IOS, self::APPTYPE_ANDROID]) && $this->appVersion && version_compare($this->appVersion,
                '5.13.0', '<')) {
            return $this->setSuccess((object)$consult_info);
        }

        //兼容小程序低版本
        if($this->appType == self::APPTYPE_STORE_MINIPROGRAM && empty($this->appVersion)){
            return $this->setSuccess((object)$consult_info);
        }

        //获取最后一次留资,门店留资默认显示最近一次留资电话，不区分门店
        $last_consult_info = StoreUserConsult::getUserLastConsultInfo($this->uid, $store_id);
        if(!empty($last_consult_info)){
            $consult_info['phone'] = $last_consult_info['phone'];
            $consult_info['area'] = $last_consult_info['city'];
            $consult_info['city_id'] = $last_consult_info['city_id'];
            $consult_info['area_code'] = $last_consult_info['area_code'];
            $consult_info['area_name'] = $last_consult_info['area_name'];
        }

        //没有留资信息则返回用户绑定的手机号
        if (!isset($consult_info['phone']) || empty($consult_info['phone'])) {

           /* //获取用户绑定的手机号
            $enc_phone = Phone::getUserPhone($this->uid);
            // 微信小程序特殊处理
            if (self::APPTYPE_STORE_MINIPROGRAM === $this->appType) {
                if (!$enc_phone) {
                    $enc_phone = WxaPhoneModel::getUserPhone($this->uid);
                }
                if (!$enc_phone) {
                    $enc_phone = WxaModel::getUserPhone($this->uid);
                }
            }
            $decrypt_phone = null;
            if (!empty($enc_phone)) {
                $decrypt_phone = PhoneCrypto::decrypt($enc_phone, self::DECRYPT_PHONE, PhoneCrypto::VERSIN);
            }*/
            $decrypt_phone = Phone::getDecryptPhoneByUid($this->uid, \hhz\constant\basic\findPhoneScene::STORE_USER_CONSULT_PHONE);
            if (!empty($decrypt_phone)) {
                //将客户端加密的手机号返回
                $crypt_phone = Crypt::encryptByPhone($decrypt_phone);
            }

            $consult_info['phone'] = $crypt_phone;
        } else {
            $consult_info['phone'] = Crypt::encryptByPhone($consult_info['phone']);
        }

        return $this->setSuccess((object)$consult_info);
    }


    public function getSaleList($store_id)
    {
        $result = [];
        $saleInfo = StoreHomeService::getStoreSaleInfo($store_id);
        if ($saleInfo && is_array($saleInfo)) {
            foreach ($saleInfo as $val) {
                $result[] = [
                    'title' => '门店活动',
                    'desc' => $val['content']
                ];
            }
        }
        return $result;
    }


}